﻿using System;
using System.Collections.Concurrent;
using System.Reflection;

namespace CQRSFramework.Infrustructures.Impl
{
    public class TypeNameProvider : ITypeNameProvider
    {
        private readonly ConcurrentDictionary<string, Type> dic = new ConcurrentDictionary<string, Type>();
        public TypeNameProvider(Assembly[] assemblies)
        {
            foreach (var assembly in assemblies)
            {
                foreach (var type in assembly.GetExportedTypes())
                {
                    dic.TryAdd(GetTypeName(type), type);
                }
            }
        }

        public Type Get(string name)
        {
            Type type;
            if (!dic.TryGetValue(name, out type))
            {
                return null;
            }
            return type;
        }

        public string GetTypeName(Type type)
        {
            return type.FullName;
        }
    }
}
